package com.tisson.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.tisson.database.FormatUtil;
import com.tisson.database.StatementUtil;
import com.tisson.pojo.AcDepartment;
import com.tisson.pojo.AdminRight;

public class AdminRightDAO {
	private String query;
	private ResultSet rs;
	private Statement stmt;

	public AdminRight findById(String id) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_right where id = '" + id + "'";
		rs = stmt.executeQuery(query);
		AdminRight ar = new AdminRight();
		while (rs.next()) {
			ar.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			ar.setAdminId(FormatUtil.ConvertUuid(rs.getBytes("admin_id")));
			ar.setFuncId(FormatUtil.ConvertUuid(rs.getBytes("func_id")));
		}
		rs.close();
		stmt.close();
		return ar;
	}

	public List findByAdminId(String id) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_right where admin_id = '" + id + "'";
		rs = stmt.executeQuery(query);
		List a = new ArrayList();
		while (rs.next()) {
			AdminRight ar = new AdminRight();
			ar.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			ar.setAdminId(FormatUtil.ConvertUuid(rs.getBytes("admin_id")));
			ar.setFuncId(FormatUtil.ConvertUuid(rs.getBytes("func_id")));
			a.add(ar);
		}
		rs.close();
		stmt.close();
		return a;
	}

	public List findAll() throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_right";
		rs = stmt.executeQuery(query);
		List list = new ArrayList();
		while (rs.next()) {
			AdminRight ar = new AdminRight();
			ar.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			ar.setAdminId(FormatUtil.ConvertUuid(rs.getBytes("admin_id")));
			ar.setFuncId(FormatUtil.ConvertUuid(rs.getBytes("func_id")));
			list.add(ar);
		}
		rs.close();
		stmt.close();
		return list;
	}

	public void update(AdminRight ar) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "update admin_right set " + "admin_id = '" + ar.getAdminId()
				+ "',func_id = '" + ar.getFuncId() + "' " + "where id = '"
				+ ar.getId() + "'";
		stmt.executeUpdate(query);
		stmt.close();
	}

	public void save(AdminRight ar) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "insert into admin_right(admin_id, func_id) values('"
				+ ar.getAdminId() + "','" + ar.getFuncId() + "')";
		stmt.executeUpdate(query);
		stmt.close();
	}

	public void delete(AdminRight ar) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "delete from admin_right where id = '" + ar.getId() + "'";
		stmt.executeUpdate(query);
		stmt.close();
	}

}
